﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using System.Collections;
using Microsoft.Office.Interop.Excel;

using Fingu.XJ.XML;
using Fingu.XJ.SQL;
using Fingu.XJ.Excel;
using Fingu.XJ.TRM045.Export;
using System.Runtime.InteropServices;
using System.IO;

namespace Fingu.ATF.Testing
{
    public partial class frmExport : Form
    {

        #region instance obj

        clsExport tmr045Exp = new clsExport();

        #endregion

        #region defind variable

        #endregion

        public frmExport()
        {
            InitializeComponent();
        }

        private void btnExporting_Click(object sender, EventArgs e)
        {
            tmr045Exp.ClearBags();

            if (tmr045Exp.InitConnection("192.168.23.5", "mcsdbtrial", "sa", "zhaopeng") == true)
            {
                if (tmr045Exp.Connection() == true)
                {
                    tmr045Exp.SetProcductCode("tmr045tyz");//sp557tyz
                    tmr045Exp.SetProcessName("功放效率测试");//互调测试

                    if (tmr045Exp.CreateQueryConditons(txtProductID.Text + ","
                                                        + cobTestResult.Text + ","
                                                        + dtpStartDateTime.Value.ToShortDateString() + ","
                                                        + dtpStopDateTime.Value.ToShortDateString() + ","
                                                        + cobInspectionState.Text) == true)
                    {                                               
                        if (tmr045Exp.Query() == true)
                        {
                            tmr045Exp.InitProductID(tmr045Exp.GetDataTable().Rows.Count);
                            tmr045Exp.InitEmployer(tmr045Exp.GetDataTable().Rows.Count);
                            tmr045Exp.InitTestDateTime(tmr045Exp.GetDataTable().Rows.Count);
                            tmr045Exp.InitInspectionState(tmr045Exp.GetDataTable().Rows.Count);
                            tmr045Exp.SetProcductCode(tmr045Exp.GetDataTable().Rows[0][3].ToString());
                            tmr045Exp.SetTestDateTime2(DateTime.Now);

                            for (int i = 1; i <= tmr045Exp.GetDataTable().Rows.Count; i++)
                            {
                                tmr045Exp.SetPorductID(tmr045Exp.GetDataTable().Rows[i - 1][1].ToString(), i - 1);
                                tmr045Exp.SetEmployer(tmr045Exp.GetDataTable().Rows[i - 1][4].ToString(), i - 1);
                                tmr045Exp.SetTestDateTime(tmr045Exp.GetDataTable().Rows[i - 1][7].ToString(), i - 1);
                                tmr045Exp.SetInspectionState(tmr045Exp.GetDataTable().Rows[i - 1][8].ToString(), i - 1);

                                if (tmr045Exp.LoadXML(tmr045Exp.GetDataTable().Rows[i - 1][6].ToString()) == true)
                                {
                                    if (tmr045Exp.AnalyzeXML() == true)
                                    {
                                    }
                                }
                            }

                            tmr045Exp.SetStartCell("1");

                            tmr045Exp.SetStartColumn("1");

                            tmr045Exp.SetStopCell("");

                            tmr045Exp.SetStopColumn("");

                            if (tmr045Exp.Create() == true)
                            {
                                tmr045Exp.SetSheetPage(1);

                                //if (WriteIn(GetDataTable().Rows[0][1].ToString(), 
                                //            (string[])GetXmlDataBags()[0],
                                //            GetDataTable().Rows[0][4].ToString(), 
                                //            GetDataTable().Rows[0][7].ToString(),
                                //            GetDataTable().Rows[0][8].ToString()) == true)
                                //{
                                //    if (SavaAs("") == true)
                                //    {
                                //        if (Close() == true)
                                //        {
                                //        }
                                //    }
                                //}

                                if (tmr045Exp.WriteIn(tmr045Exp.GetProductID(),
                                                    tmr045Exp.GetXmlDataBags(),
                                                    tmr045Exp.GetEmployer(),
                                                    tmr045Exp.GetTestDateTime(),
                                                    tmr045Exp.GetInspectionState()) == true)
                                {
                                    if (tmr045Exp.SavaAs(Directory.GetCurrentDirectory()
                                                        + @"\"
                                                        + tmr045Exp.GetProductCode()
                                                        + "_"
                                                        + tmr045Exp.GetTestDateTime2()) == true)
                                    {
                                        if (tmr045Exp.Close() == true)
                                        {
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void frmExport_FormClosing(object sender, FormClosingEventArgs e)
        {
            tmr045Exp.DisConnection();
        }
    }
}
